var datatable;// 定义一个全局的dataTable
var attachChar;
var epsAttachChar;
var areaAttachChar;
var d;
//第二版变更开始**********
//查询参数
var param = new Object({
	init:function(){
		this.timeSize = ''
		this.beginDate = ''
		this.endDate = ''
		this.netYuan = ''
		this.dimension = ''
		this.area = ''
		this.tac_status=0	
		this.tac=''
		this.eNodeBID_status=0	
		this.eNodeBID = ''
		this.village_status=0	
		this.village=''
		this.userGroup = ''
		this.user = ''
		this.terminal_type = ''
		this.terminal_name = ''
		this.apn = ''
	},
	setDimensionParam:function(dimension){
		switch(dimension) {
			case 'area' :
				this.area=$('#area').val()
				if($("#tac").is(':checked')){
				  this.tac=$("#tac_value").val();
				  this.tac_status=1;
			    }
				if($("#eNodeBID").is(':checked')){
					this.eNodeBID=$("#eNodeBID_value").val();
					this.eNodeBID_status=1;
				}
				if($("#village").is(':checked')){
					this.village=$("#village_value").val();
					this.village_status=1;
				}
				break
			case 'userGroup' :
				this.userGroup = $('#userGroup').val()
				break
			case 'user' :
				this.user = $('#user').val()
				break
			case 'terminal' :
				this.terminal_type = $('#terminal_type').val()
				this.terminal_name = $('#terminal_name').val()
				break
			case 'apn' :
				this.apn = $('#apn').val()
				break
		}
	},
	setParam:function(){
		this.init()
		this.timeSize = $('input:radio[name="timeSize"]:checked').val();
		this.beginDate = $('#beginDate').val()
		this.endDate = $('#endDate').val()
		this.netYuan = $('#netYuan').val()
		this.dimension = $('input:radio[name="latitude"]:checked').val();
		this.setDimensionParam(this.dimension)
	}
});
//第二版变更结束**********
$(function() {
	//网元下拉
	$("#netYuan").combobox();
	//小区下拉
    //$("#village_value").combobox();
    //用户组下拉
    $("#userGroup").combobox();
    //apn下拉
    $("#apn").combobox();
    //终端
    $("#terminal_type").combobox({
		select:function(event,ui){
			var terminalBrand = $(this).val();
			$.ajax({
				url : '../../feature/terminalUE/terminalModelInfos?terminalBrand='
						+ terminalBrand,
				type : 'POST',
				dataType : 'JSON',
				error : function() {
					alert('Error loading data!');
				},
				success : function(msg) {
					$("#terminal_name").empty();
					var list = msg.terminalModelList;
					$.each(eval(list), function(i, item) {
							$("<option value='" + item.terminal_model + "'>" + item.terminal_model+ "</option>").appendTo($("#terminal_name"));
					});
				}
			});
		},
		width:100
	});
    $("#terminal_name").combobox();
	//初始化table
    if($("#t").val()=="1"){
    	getList();
    }

	$('#table tbody').on('click', 'tr', function() {
		if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
		} else {
			datatable.$('tr.selected').removeClass('selected');
			$(this).addClass('selected');
		}
	});
	
	showArearList("area");
	
	//初始化图表
	psLineChar = echarts.init(document.getElementById('psLine'));
	
	$("input[name='latitude']").change(function(){
		if($(this).val()=="area"){
			$("#eNodeBID").attr("disabled",false);
			$("#tac").attr("disabled",false);
			$("#village").attr("disabled",false);
			
			$("#eNodeBID_value").attr("readonly",false);
			$("#tac_value").attr("readonly",false);
			$("#village_value").attr("readonly",false);
		}else{
			$("#eNodeBID").attr("disabled",true);
			$("#tac").attr("disabled",true);
			$("#village").attr("disabled",true);
			
			$("#eNodeBID_value").attr("readonly",true);
			$("#tac_value").attr("readonly",true);
			$("#village_value").attr("readonly",true);
		}
	})
	if($("#jspType").val() == 'qwanalysis'){
		$("input[type='radio'][name=latitude][value='area']").attr("checked",true);
		$("input[type='radio'][name=timeSize][value='day']").attr("checked",true);
		$("#eNodeBID").attr("checked", true); 
		$("#eNodeBID_value").val($("#enodeb_id").val());
		$("#area").val($("#city_code").val());
		$("#area option[text="+$("#city_code").val()+"]").attr("selected", true);
	}
	if($("#jspType").val() == 'kpi_analysis'){
		var kpi_timeSize = $('#kpi_timeSize').val();
		$("input[type='radio'][name=timeSize][value='"+kpi_timeSize+"']").attr("checked",true);
	}
})

function getList() {
	
	var startTime=$("#beginDate").val();  
    var endTime=$("#endDate").val();  
    if(!checkEndTime()){
		layer.alert("开始时间不能大于结束时间！", {
			icon : 3,
			offset: '100px'
		});
		return;
	};
	
	//第二版变更开始**********
	param.setParam();
	
	//第二版变更结束**********
	//$("#table tbody tr").remove();
	$("#tbodyId").remove();
	$('#table').append($("<tbody id='tbodyId'></tbody>"));
	if (datatable == undefined || datatable == "undefined" || datatable == null) {
	} else {
		datatable.destroy();
	}
	datatable = $('#table')
			.DataTable(
					{
						"aoColumnDefs" : [ {
							sDefaultContent : '',
							aTargets : [ '_all' ]
						} ],
						"bPaginate" : true, // 翻页功能
						"iDisplayLength" : 50,
						"bLengthChange" : false, // 改变每页显示数据数量
						"bFilter" : false, // 过滤功能
						"ordering": true, //排序功能
						"bSort" : true, // 排序功能
						"bInfo" : true,// 页脚信息
						"bAutoWidth" : true,// 自动宽度
						"iDisplayLength" : 50,
						// "sPaginationType": "full_numbers",//显示分页的样式
						// 还有simple_numbers等
//						"sScrollX" : "100%",
						"sScrollXInner" : "110%",
						"bScrollCollapse" : true,
						"oLanguage" : {
							"sLengthMenu" : "每页显示 _MENU_条",
							"sZeroRecords" : "没有找到符合条件的数据",
							"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
							"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
							"sInfoEmpty" : "没有记录",
							"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
							"sSearch" : "搜索：",
							"oPaginate" : {
								"sFirst" : "首页",
								"sPrevious" : "前一页",
								"sNext" : "后一页",
								"sLast" : "尾页"
							}
						},
						"bProcessing" : true,
						"bServerSide" : true,
						"sAjaxSource" : "../../businessAnalysis/attachment/getAttachment",
						"fnServerData" : function(sSource, aoData, fnCallback) {
							$
									.ajax({
										"type" : "post",
										"contentType" : "application/x-www-form-urlencoded",
										"url" : sSource,
										"dataType" : "json",
										"data" : {
											"aoData" : JSON.stringify(aoData),
											"param" : JSON.stringify(param)
										}, // 以json格式传递
										"success" : function(resp) {
											fnCallback(resp.pageDate);
											d = resp.allData;
										}
									});
						},
						columns : [
								{
									data : "report_date"
								},
								{
									data : "mme_name"
								},
								//第二版变更开始**********
								{
									data : "city_code",
									"visible": false
								},
								{
									data : "tai",
									"visible": false
								},
								{
									data : "enodeb_id",
									"visible": false
								},
								{
									data : "enodeb_name",
									"visible": false
								},
								{
									data : "cell_name",
									"visible": false
								},
								{
									data : "group_id",
									"visible": false
								},
								{
									data : "msisdn",
									"visible": false
								},
								{
									data : "user_terminal",
									"visible": false
								},
								{
									data : "apn_name",
									"visible": false
								},
								//第二版变更结束**********
								{
									data : "atta_diffnet_count"
								},
								{
									data : "eps_atta_count"
								},
								{
									data : "eps_atta_success_count"
								},
								{
									data : "eps_atta_failure_count",
									render : function(value, type, row) {
										var dateType = $('input:radio[name="timeSize"]:checked').val();
										var failureType = 1;
										var jspType = "attachment";
										var paramDate = row.report_date == undefined ? "" : row.report_date;
										var mme_name = row.mme_name == undefined ? "" : row.mme_name;
										var city_code = row.city_code == undefined ? "" : row.city_code;
										var tai = row.tai == undefined ? "" : row.tai;
										var enodeb_id = row.enodeb_id == undefined ? "" : row.enodeb_id;
										var enodeb_name =row.enodeb_name == undefined ? "" : row.enodeb_name;
										var cell_name =row.cell_name == undefined ? "" : row.cell_name;
										var group_id = row.group_id == undefined ? "" : row.group_id;
										var msisdn = row.msisdn == undefined ? "" : row.msisdn;
										var user_terminal = row.user_terminal == undefined ? "" : row.user_terminal;
										var terminal_brand = "";
										var apn_name = row.apn_name == undefined ? "" : row.apn_name;
										
										if(value==null){
											value="";
										}
										return "<a href='#' style=\"text-decoration: underline;\" onclick=\"forwordInterfaceFailure('"
												+ dateType + "','"
												+ failureType +"','"
												+ jspType +"','"
												+ mme_name +"','"
												+ city_code +"','"
												+ tai +"','"
												+ enodeb_id +"','"
												+ enodeb_name +"','"
												+ cell_name +"','"
												+ group_id +"','"
												+ msisdn +"','"
												+ user_terminal +"','"
												+ terminal_brand +"','"
												+ apn_name +"','"
												+ paramDate
												+ "')\"  >" + value + "</a>";
									}
								},
								{
									data : "eps_atta_noresp_count"
								},
								{
									data : "eps_atta_time_delay"
								},
								{
									data : "eps_atta_success_rate"
								}, /*
									 * { data : "eps_atta_failure_rate" },
									 */
								{
									data : "joint_atta_count"
								},
								{
									data : "joint_atta_success_count"
								},
								{
									data : "joint_atta_failure_count",
									render : function(value, type, row) {
										var dateType = $('input:radio[name="timeSize"]:checked').val();
										var failureType = 2;
										var jspType = "attachment";
										var paramDate = row.report_date == undefined ? "" : row.report_date;
										var mme_name = row.mme_name == undefined ? "" : row.mme_name;
										var city_code = row.city_code == undefined ? "" : row.city_code;
										var tai = row.tai == undefined ? "" : row.tai;
										var enodeb_id = row.enodeb_id == undefined ? "" : row.enodeb_id;
										var enodeb_name =row.enodeb_name == undefined ? "" : row.enodeb_name;
										var cell_name =row.cell_name == undefined ? "" : row.cell_name;
										var group_id = row.group_id == undefined ? "" : row.group_id;
										var msisdn = row.msisdn == undefined ? "" : row.msisdn;
										var user_terminal = row.user_terminal == undefined ? "" : row.user_terminal;
										var terminal_brand = "";
										var apn_name = row.apn_name == undefined ? "" : row.apn_name;
										if(value==null){
											value="";
										}
										return "<a href='#' style=\"text-decoration: underline;\" onclick=\"forwordInterfaceFailure('"
												+ dateType + "','"
												+ failureType +"','"
												+ jspType +"','"
												+ mme_name +"','"
												+ city_code +"','"
												+ tai +"','"
												+ enodeb_id +"','"
												+ enodeb_name +"','"
												+ cell_name +"','"
												+ group_id +"','"
												+ msisdn +"','"
												+ user_terminal +"','"
												+ terminal_brand +"','"
												+ apn_name +"','"
												+ paramDate
												+ "')\"  >" + value + "</a>";
									}
								},
								{
									data : "joint_atta_noresp_count"
								},
								{
									data : "joint_atta_time_delay"
								},
								{
									data : "joint_atta_success_rate"
								}, /*
									 * { data : "joint_atta_failure_rate" },
									 */
								{
									data : "emer_atta_count"
								},
								{
									data : "emer_atta_success_count"
								},
								{
									data : "emer_atta_failure_count",
									render : function(value, type, row) {
										var dateType = $('input:radio[name="timeSize"]:checked').val();
										var failureType = 3;
										var jspType = "attachment";
										var paramDate = row.report_date == undefined ? "" : row.report_date;
										var mme_name = row.mme_name == undefined ? "" : row.mme_name;
										var city_code = row.city_code == undefined ? "" : row.city_code;
										var tai = row.tai == undefined ? "" : row.tai;
										var enodeb_id = row.enodeb_id == undefined ? "" : row.enodeb_id;
										var enodeb_name =row.enodeb_name == undefined ? "" : row.enodeb_name;
										var cell_name =row.cell_name == undefined ? "" : row.cell_name;
										var group_id = row.group_id == undefined ? "" : row.group_id;
										var msisdn = row.msisdn == undefined ? "" : row.msisdn;
										var user_terminal = row.user_terminal == undefined ? "" : row.user_terminal;
										var terminal_brand = "";
										var apn_name = row.apn_name == undefined ? "" : row.apn_name;
										if(value==null){
											value="";
										}
										return "<a href='#' style=\"text-decoration: underline;\" onclick=\"forwordInterfaceFailure('"
												+ dateType + "','"
												+ failureType +"','"
												+ jspType +"','"
												+ mme_name +"','"
												+ city_code +"','"
												+ tai +"','"
												+ enodeb_id +"','"
												+ enodeb_name +"','"
												+ cell_name +"','"
												+ group_id +"','"
												+ msisdn +"','"
												+ user_terminal +"','"
												+ terminal_brand +"','"
												+ apn_name +"','"
												+ paramDate
												+ "')\"  >" + value + "</a>";
									}
								}, {
									data : "emer_atta_noresp_count"
								}, {
									data : "emer_atta_time_delay"
								}, {
									data : "emer_atta_success_rate"
								} /*
									 * , { data : "emer_atta_failure_rate" }
									 */]

					});
	//第二版变更开始**********
	var a=$("input[name='latitude']:checked");
	if(a.attr('data-column')!=undefined){
		if(a.val()=="area"){
			if($("#tac").is(':checked')){
				var column = datatable.column($("#tac").attr('data-column'));
			    column.visible(true);
			}
			if($("#eNodeBID").is(':checked')){
				var column = datatable.column($("#eNodeBID").attr('data-column'));
			    column.visible(true);
			}
			if($("#village").is(':checked')){
				var column = datatable.column($("#village").attr('data-column'));
			    column.visible(true);
			}
		}
		var column = datatable.column(a.attr('data-column'));
	    column.visible(true);
	}
	//第二版变更结束**********
}

function showPic() {

	var tab = $("#con");
	var pic = $("#pic");

	if (tab.is(":hidden")) {
		tab.show();
		pic.hide();
	} else {
		tab.hide();
		pic.show();

		initAttachChar();
	}
}

function initAttachChar() {
	// 查询
	psLineChar.clear();
	psLineChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../businessAnalysis/attachment/getAttachChar?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"allData" : JSON.stringify(d)
		},
		success : function(data) {
			psLineChar.setOption(data, true);
			psLineChar.hideLoading();
		}
	});
}

function resetQueryForm() {
	$('#queryForm')[0].reset();

	$("#eNodeBID").attr("disabled",true);
	$("#tac").attr("disabled",true);
	$("#village").attr("disabled",true);
	
	$("#eNodeBID_value").attr("readonly",true);
	$("#tac_value").attr("readonly",true);
	$("#village_value").attr("readonly",true);
}

function exportExcel() {
	$.ajax({
		type : "POST",
		url : "../../businessAnalysis/attachment/exportBigDataExcel?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"param" : JSON.stringify(param)
		},
		success : function(data) {
			if (data.flag) {
				var url = "../../businessAnalysis/attachment/downFiles?path="
						+ data.path;
				window.location.href = url;
			} else {
				alert(data.msgs);
			}
		}
	});
}

function forwordInterfaceFailure(dateType,failureTypeInfo,jspType,mme_name,city_code,tai,
		enodeb_id,enodeb_name,cell_name,group_id,msisdn,user_terminal,terminal_brand,apn_name,paramDate) {
	var url = "/kpi/interfaceFailureLink/index?dateType=" + dateType +"&failureTypeInfo="+failureTypeInfo + "&jspType="+jspType +"&mme_name="+mme_name
	+ "&paramDate=" + paramDate + "&interfaceType=2&signalType=1&type=" + param.dimension +"&tac_status="+param.tac_status
	+ "&eNodeBID_status=" + param.eNodeBID_status +"&village_status="+param.village_status
	+ "&city_code=" +city_code+"&tai="+tai+"&enodeb_id="+enodeb_id+"&enodeb_name="+enodeb_name  
	+ "&cell_name=" +cell_name+"&group_id="+group_id+"&msisdn="+msisdn+"&user_terminal="+user_terminal+"&terminal_brand="+terminal_brand+"&apn_name="+apn_name;   
	linkTab("接口故障", url);
}

// 下拉树 begin
function onClick(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	nodes = zTree.getSelectedNodes(),
	v = "";
	nodes.sort(function compare(a,b){return a.id-b.id;});
	for (var i=0, l=nodes.length; i<l; i++) {
		v += nodes[i].name + ",";
	}
	if (v.length > 0 ) v = v.substring(0, v.length-1);
	var cityObj = $("#citySel");
	cityObj.attr("value", v);
	$("#area").val(treeNode.id);
	
	hideMenu();
}
function showMenu() {
	var cityObj = $("#citySel");
	var cityOffset = $("#citySel").offset();
	$("#menuContent").css({left:cityOffset.left-26, top:cityOffset.top-106}).slideDown("fast");

	$("body").bind("mousedown", onBodyDown);
}
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
		hideMenu();
	}
}
//下拉树 end

function checkEndTime(){  
    var startTime=$("#beginDate").val();  
    var start=new Date(startTime.replace("-", "/").replace("-", "/"));  
    var endTime=$("#endDate").val();  
    var end=new Date(endTime.replace("-", "/").replace("-", "/"));  
    if(end<start){  
        return false;  
    }  
    return true;  
}